import 'package:flutter/material.dart' ;
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mfkb/plugin/MyBehavior.dart';
import 'package:mfkb/service/FunService.dart';
import 'package:toast/toast.dart';


class Relation extends StatefulWidget {
  @override
  _RelationState createState() => _RelationState();
}

class _RelationState extends State<Relation> {

  List keys = [] ;
  List select = [] ;
  dynamic data = null ;

  @override
  void initState() {
    super.initState();
    FunService().relations().then((value){
      if(value["code"] == 200){
        data = value["data"] ;
        keys = value["data"].keys.toList() ;
        for(var i = 0 ; i < keys.length ; i++){ 
          var t = {"selected":true , "icon":Image.asset("assets/images/more_2x.png" , fit: BoxFit.fill,) , "height":56 + 56*data[keys[i]].length} ; 
          select.add(t) ;
        }
        setState(() {});
      }else{
        Toast.show(value["msg"], context) ;
      }
    }) ;
  }

  @override
  Widget build(BuildContext context) { 
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark ,
      child: Scaffold(
        body: Container(
          color:Color.fromARGB(255, 245, 246, 248) ,
          child: Column(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).padding.top ,
              ) ,
              Container(
                height: 45,
                child: Row(
                  children: <Widget>[
                    Container(
                      width: 17,
                      height: 17,
                      margin: EdgeInsets.only(left:ScreenUtil().setWidth(19)),
                      child: GestureDetector(
                        child: Container(
                          width: 17,
                          height: 17,
                          child: Image.asset("assets/images/back.png" , fit: BoxFit.fill,),
                        ),
                        onTap: (){
                          Navigator.of(context).pop() ;
                        },
                      ),
                    ) ,
                    Expanded(
                      child: Container(
                        child: Center(
                          child: Text("联系方式" , style: TextStyle( 
                            color: Color.fromARGB(255, 19 , 28 , 53) , 
                            fontSize: ScreenUtil().setSp(18) , 
                            fontWeight: FontWeight.bold  
                          ),), 
                        ),
                      ),
                    ) ,
                    Container(
                      height: 17 ,
                      width: 36 ,
                      margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)) ,
                    ) 
                  ],
                ),
              ) ,
              Expanded(
                child: Container(
                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(10) , left:ScreenUtil().setWidth(10) , right:ScreenUtil().setWidth(10) , bottom: ScreenUtil().setHeight(10)),
                  decoration: BoxDecoration(
                  //  color: Colors.white ,
                  //  borderRadius: BorderRadius.all(Radius.circular(16)) 
                  ) ,
                  child: Column(
                    children: <Widget>[
                      Container() ,
                      Expanded(
                        child: ScrollConfiguration(
                          behavior: MyBehavior() ,
                          child: ListView(
                            padding: EdgeInsets.all(0) ,
                            children: List.generate(keys == null ? 0 : keys.length , (index){
                              return GestureDetector(
                                child: Container(
                                  height:ScreenUtil().setHeight(select[index]["height"]) ,
                                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(index == 0 ? 0 : 12)) ,
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(8)) ,
                                    color: Colors.white ,
                                  ),
                                  child: Column(
                                    children: <Widget>[
                                      Expanded(
                                        child: Container(
                                          child: Row(
                                            children: <Widget>[
                                              Expanded(
                                                child: Container(
                                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                                                  child: Text(keys[index] , style: TextStyle(
                                                    fontSize: ScreenUtil().setSp(15) ,
                                                    color: Color.fromARGB(255, 78, 86, 108) ,
                                                    fontWeight: FontWeight.bold 
                                                  ),overflow: TextOverflow.ellipsis,),
                                                ),
                                              ) ,
                                              Container(
                                                margin: EdgeInsets.only(right:ScreenUtil().setWidth(10)),
                                                width: ScreenUtil().setHeight(16),
                                                height: ScreenUtil().setHeight(16), 
                                                child: select[index]["icon"] , 
                                              )
                                            ],
                                          ),
                                        ),
                                      ) ,
                                      Container(
                                        height: ScreenUtil().setHeight(1),
                                        color: Color.fromARGB(255, 236, 237, 241) ,
                                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(13) , right:ScreenUtil().setWidth(12)),
                                      ) ,
                                      Visibility(
                                        visible: select[index]["selected"],
                                        child: Container(
                                          height: ScreenUtil().setHeight(select[index]["height"] - 56),
                                          child: Column(
                                            children: List.generate(data[keys[index]].length, (i){
                                              return Container(
                                                height: ScreenUtil().setHeight(56) ,
                                                child: Column(
                                                  children: <Widget>[
                                                    Container(
                                                      height: ScreenUtil().setHeight(55),
                                                      child: Row(
                                                        children: <Widget>[
                                                          Expanded(
                                                            child: Container(
                                                              margin: EdgeInsets.only(left:ScreenUtil().setWidth(14)),
                                                              child: Text(data[keys[index]][i]["relationName"] , style: TextStyle(
                                                                fontSize: ScreenUtil().setSp(14) ,
                                                                color: Color.fromARGB(255, 121, 132, 155)
                                                              ),),
                                                            ),
                                                          ) ,
                                                          Container(
                                                            width: ScreenUtil().setWidth(100),
                                                            margin: EdgeInsets.only(right:ScreenUtil().setWidth(13)),
                                                            child: Text(data[keys[index]][i]["relationNumber"] , style: TextStyle(
                                                              fontSize: ScreenUtil().setSp(12) ,
                                                              color: Color.fromARGB(255, 19, 28, 53)
                                                            ),textAlign: TextAlign.end,),
                                                          ) ,
                                                          GestureDetector(
                                                            child: Container(
                                                              width: ScreenUtil().setWidth(37),
                                                              height: ScreenUtil().setHeight(18),
                                                              margin: EdgeInsets.only(right:ScreenUtil().setWidth(12)),
                                                              decoration: BoxDecoration(
                                                                color: Color.fromARGB(255, 242, 243, 247) ,
                                                                borderRadius: BorderRadius.all(Radius.circular(9)) ,
                                                              ),
                                                              child: Center(
                                                                child: Text("复制" , style: TextStyle(
                                                                  fontSize: ScreenUtil().setSp(10) ,
                                                                  color: Color.fromARGB(255, 121, 132, 155)
                                                                ),),
                                                              ),
                                                            ),
                                                            onTap: (){
                                                              String text = data[keys[index]][i]["relationNumber"] ;
                                                              ClipboardData d = new ClipboardData(text:text);
                                                              Clipboard.setData(d);
                                                              Toast.show("复制成功", context) ;
                                                            },
                                                          )
                                                        ],
                                                      ),
                                                    ) ,
                                                    Container(
                                                      height: ScreenUtil().setHeight(1),
                                                      color: Color.fromARGB(255, 236, 237, 241) ,
                                                      margin: EdgeInsets.only(left:ScreenUtil().setWidth(13) , right:ScreenUtil().setWidth(12)),
                                                    ) ,
                                                  ],
                                                ),
                                              ) ;
                                            }) ,
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                                onTap: (){
                                  setState(() {
                                    if(select[index]["selected"]){
                                      select[index]["icon"] = Image.asset("assets/images/more_2x.png" , fit: BoxFit.fill,) ;
                                      select[index]["height"] = 56 ;
                                    }else{
                                      select[index]["icon"] = Image.asset("assets/images/more_down.png" , fit: BoxFit.fill,) ;
                                      select[index]["height"] = 56 + 56*data[keys[index]].length ;
                                    }
                                    select[index]["selected"] = !select[index]["selected"] ;
                                  });
                                },
                              ) ;
                            }) ,
                          ) ,
                        ) ,
                      )
                    ],
                  )
                ) ,
              )
            ],
          ),
        ),
      ),
    );
  }
}